﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 获取他人的活动列表
    /// </summary>
    class GetActivityList4OtherAction : GetAction<Params.GetActivityList4OtherParams,
        Results.GetActivityList4OtherResult>
    {
        protected override ActionResult<Results.GetActivityList4OtherResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            Params.GetActivityList4OtherParams opParams)
        {
            int limit = 10;
            int start = opParams.Page * limit;
            ActivityService service = new ActivityService();
            
            ServiceResult<PagedList<Activity>> serviceResult = null;
            if (opParams.Type == 0)
            {
                return new ActionResult<Results.GetActivityList4OtherResult>(false, ErrorCode.Common_InvalidParameter, "不能查看他人进行中的活动", null);
            }
            else if (opParams.Type == 1)
            {
                serviceResult = new ActivityService().GetFinishedActivityList4User(opParams.TargetId, start, limit);
            }
            else if (opParams.Type == 2)
            {
                serviceResult = new ActivityService().GetUnfinishedActivityList4Creator(opParams.TargetId, start, limit);
            }
            JizSport.Common.Logger.DebugToTag(LogTag.TAG, "GetActivityList4OtherAction get list type:{0} page:{1} targetid:{2}", opParams.Type, opParams.Page, opParams.TargetId);

            Results.GetActivityList4OtherResult actionResult = new Results.GetActivityList4OtherResult();

            if (serviceResult != null)
            {
                if (serviceResult.ErrorCode == ErrorCode.NoError)
                {
                    actionResult.Page = opParams.Page;
                    actionResult.Start = serviceResult.Data.Start;
                    actionResult.Limit = serviceResult.Data.Limit;
                    actionResult.Total = serviceResult.Data.Total;

                    for (int index = 0; index < serviceResult.Data.Items.Count; index++)
                    {
                        Activity activity = serviceResult.Data.Items[index];

                        Results.GetActivityList4OtherResult.ActivityItem item = new Results.GetActivityList4OtherResult.ActivityItem();

                        item.Id = activity.Id;
                        item.Title = activity.Title;
                        item.CategoryCode = activity.CategoryCode;
                        item.Longitude = activity.Longitude;
                        item.Latitude = activity.Latitude;
                        item.MaxPersonCount = activity.MaxPersonCount;
                        item.CurrentPersonCount = activity.CurrentPersonCount;
                        item.BeginTime = activity.BeginTime;
                        item.EndTime = activity.EndTime;
                        item.IsFinished = activity.IsFinished;
                        item.Address = activity.Address;

                        actionResult.Items.Add(item);
                    }

                    if (actionResult.Start + actionResult.Items.Count >= actionResult.Total)
                    {
                        actionResult.IsTheEnd = true;
                    }
                    else
                    {
                        actionResult.IsTheEnd = false;
                    }

                    return new ActionResult<Results.GetActivityList4OtherResult>("1.0.1", true, serviceResult.ErrorCode,
                        ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
                }
                return new ActionResult<Results.GetActivityList4OtherResult>("1.0.1", false, serviceResult.ErrorCode,
                        ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetActivityList4OtherResult>("1.0.1", false, ErrorCode.Common_InvalidParameter,
                    ErrorCode.ToString(ErrorCode.Common_InvalidParameter), actionResult);
        }
    }
}
